package com.qf.controller;

import com.qf.pojo.PageBean;
import com.qf.pojo.TbGoods;
import com.qf.pojo.TbUser;
import com.qf.service.GoodsService;
import com.qf.service.impl.GoodsServiceImpl;
import com.qf.utils.StringUtil;
import com.sun.xml.internal.bind.v2.model.core.ID;

import javax.print.DocFlavor;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/*
 *@Description:
 *@author:刘哥
 *@Date:86180 2022/8/16 17:38
 */
@WebServlet("/product")
public class GoodsServlet extends BaseServlet{
    private   GoodsService goodsService=new GoodsServiceImpl();
    //    /goodsservlet?method=getGoodsListByTypeId&typeId=
    public String show(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
//        int pageNum = Integer.parseInt(request.getParameter("pageNum"));
        String pageNum = request.getParameter("currentPage");
        System.out.println("pageNum = " + pageNum);
        int currentPage=1;
        if (!StringUtil.isEmpty(pageNum)){
//            currentPage= Integer.parseInt(pageNum);
            currentPage= Integer.parseInt(pageNum);
            if (currentPage<0){
                currentPage=1;
            }
        }
        System.out.println("currentPage = " + currentPage);
        PageBean<TbGoods> pageBean= goodsService.selectByTypeId(Long.parseLong(id),currentPage);
        System.out.println("pageBean = " + pageBean);
        request.setAttribute("pageBean", pageBean);
        return "/goodsList.jsp";

    }
//    /product?method=detail&id=${g.id}">${g.name}、
    public String detail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        long id = Long.parseLong(request.getParameter("id"));
        if (id == 0) {
            request.setAttribute("exp", "请选择商品");
            return "/error.jsp";
        }
        TbGoods tbGoods=goodsService.findById(id);
        if (tbGoods != null) {
            request.setAttribute("product",tbGoods);
            return "goodsDetail.jsp";
        }

        return "redirect:/error.jsp";
    }
//     /product?method=selectAll
    public String selectAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        TbUser admin = (TbUser) request.getSession().getAttribute("admin");
        if (admin == null) {
            return "redirect:/admin/login.jsp";
        }
        List<TbGoods> goodsList = goodsService.find();
        ServletContext servletContext = request.getServletContext();
        System.out.println("goodsList = " + goodsList);
        servletContext.setAttribute("goodsList", goodsList);

        return "redirect:/admin/showGoods.jsp";
    }

        //    /product?method=delete&id=
    public String delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        TbUser admin = (TbUser) request.getSession().getAttribute("admin");
        if (admin == null) {
            return "redirect:/admin/login.jsp";
        }
        String id = request.getParameter("id");
        goodsService.deleteGoods(Long.parseLong(id));

        return "redirect:/product?method=selectAll";
    }
    //   /product?method=selectGoods&id=
    public String selectGoods(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        TbUser admin = (TbUser) request.getSession().getAttribute("admin");
        if (admin == null) {
            return "redirect:/admin/login.jsp";
        }
        String id = request.getParameter("id");
        TbGoods goods = goodsService.findByGid(Long.parseLong(id));
        request.setAttribute("goods", goods);
        return "/admin/updateGoods.jsp";
    }
//    /product?method=update&id=
    public String update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        TbUser admin = (TbUser) request.getSession().getAttribute("admin");
        if (admin == null) {
            return "redirect:/admin/login.jsp";
        }
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String picture = request.getParameter("picture");
        String price = request.getParameter("price");
        String intro = request.getParameter("intro");
        String typeId = request.getParameter("typeId");
        TbGoods goods=new TbGoods();
        goods.setId(Long.parseLong(id));
        goods.setName(name);
        goods.setPicture(picture);
        goods.setPrice(Long.parseLong(price));
        goods.setIntro(intro);
        goods.setTypeid(Long.parseLong(typeId));
        goodsService.updateGoods(goods);

        return "redirect:/product?method=selectAll";
    }
//    /product?method=add
    public String add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        TbUser admin = (TbUser) request.getSession().getAttribute("admin");
        if (admin == null) {
            return "redirect:/admin/login.jsp";
        }
        String name = request.getParameter("name");
        String picture = request.getParameter("picture");
        String star = request.getParameter("star");
        String price = request.getParameter("price");
        String intro = request.getParameter("intro");
        String typeId = request.getParameter("typeId");
        TbGoods goods=new TbGoods();
        goods.setStar(Long.parseLong(star));
        goods.setName(name);
        goods.setPicture(picture);
        goods.setPrice(Long.parseLong(price));
        goods.setIntro(intro);
        goods.setTypeid(Long.parseLong(typeId));
        goodsService.addGoods(goods);

        return "redirect:/product?method=selectAll";
    }
}
